﻿<?php
	include '../inc/conn.php';
	include '../sendmail.php';

	session_start();
	extract($_POST);
	extract($_GET);
	$pagestr = end(explode("/",$_SERVER['PHP_SELF']));
	unset($_POST,$_GET);

	if (isset($act))
	{
		if ($act == 'add' && isset($_SESSION['logname']))
		{
			if (empty($mbname) || empty($mbver) || empty($mbreason) || empty($mblocation) || empty($mbchangedate)){
				echo "<script>alert('数据填写不能为空');window.location='$pagestr';</script>";
				die("数据填写不能为空!");
			}

			if(empty($remark))
				$remark="N/A";

			$mbreason=trim(htmlspecialchars($mbreason, ENT_QUOTES));
			$mblocation=trim(htmlspecialchars($mblocation, ENT_QUOTES));
			$remark=trim(htmlspecialchars($remark, ENT_QUOTES));

			$db = new mysql();
			$sql="INSERT INTO `hw_change` (`MB_NAME`,`MB_VER`,`MB_REASON`,`MB_LOCATION`,`MB_CHANGEDATE`,`REMARK`, `OWNER`) 
				values('${mbname}', '${mbver}', '${mbreason}', '${mblocation}', '${mbchangedate}', '${remark}', '${_SESSION['logchnname']}')";
			$db->query($sql);
			echo "<script>alert('数据添加成功!');window.location.href='/admin/hwchangelist.php'</script>";
			$db->close();
		}

		if ($act == 'change' && isset($id) && isset($_SESSION['logname']))
		{	
			$db = new mysql();

			if(empty($remark))
				$remark="N/A";

			$mbreason=trim(htmlspecialchars($mbreason, ENT_QUOTES));
			$mblocation=trim(htmlspecialchars($mblocation, ENT_QUOTES));
			$remark=trim(htmlspecialchars($remark, ENT_QUOTES));

			$table = "hw_change";
			$condition = "ID=${id}";
			$mod_content="`MB_NAME`='${mbname}', `MB_VER`='${mbver}', `MB_REASON`='${mbreason}', `MB_LOCATION`='${mblocation}', `MB_CHANGEDATE`='${mbchangedate}'
						,`REMARK`='${remark}'";
			$db->update($table, $mod_content, $condition);
			echo "<script>alert('内容修改完成!');window.location.href='/admin/hwchangelist.php';</script>";
			$db->close();
		}

		if ($act == 'mail' && isset($id) && isset($_SESSION['logname']))
		{
			$db = new mysql();
			$sql="select * from hw_change where ID=$id";
			$db->query($sql);
			$num = $db->db_num_rows();
			$row = $db->fetch_assoc();

			$content ="Dear Sirs,<br /><br />
					<b>工程维护HW变更信息明细如下：</b><br />
					<b>&nbsp;&nbsp;主板名称：</b>${row['MB_NAME']}<br />
					<b>&nbsp;&nbsp;主板版号：</b>${row['MB_VER']}<br />
					<b>&nbsp;&nbsp;变更原因：</b>${row['MB_REASON']}<br />
					<b>&nbsp;&nbsp;变更位置：</b>${row['MB_LOCATION']}<br />
					<b>&nbsp;&nbsp;变更日期：</b>${row['MB_CHANGEDATE']}<br />
					<b>&nbsp;&nbsp;管控人员：</b>${row['OWNER']}<br />
					<b>&nbsp;&nbsp;备注说明：</b>${row['REMARK']}<br />";

			$param = array(
					'aSync'=>'Yes',
					'title'=>'HW变更记录更新',
					'msg'=>$content,
					'to'=>'EE'
			);
			doAsyncMail($param);
			echo "<script>alert('邮件发送完成!');window.location.href='$pagestr';</script>";
			$db->close();
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/bootstrap-datetimepicker.min.js"></script>
<script>
	$(function(){
		$("#ADDDIV").hide();
		$("#ADDOAKEY").click(function(){
			$("#ADDDIV").toggle();
		});

		$("#addbtn").click(function(){
			if ($("#mbname").val() == ""){
				alert("主板名称没有填写！");
				return false;
			}

			if ($("#mbver").val() == ""){
				alert("主板版号没有填写！");
				return false;
			}
			
			if ($("#mbreason").val() == ""){
				alert("变更原因没有填写！");
				return false;
			}

			if ($("#mblocation").val() == ""){
				alert("变更位置没有填写！");
				return false;
			}

			if ($("#mbchangedate").val() == ""){
				alert("变更日期没有填写！");
				return false;
			}
		});
		
		$('#mbchangedate').datetimepicker({
			format: 'yyyy-mm-dd',
			weekStart: 1,
			autoclose: true,
			startView: 2,
			minView: 2,
			pickerPosition: 'bottom-right'
		});

		$('#mbchangedate1').datetimepicker({
			format: 'yyyy-mm-dd',
			weekStart: 1,
			autoclose: true,
			startView: 2,
			minView: 2,
			pickerPosition: 'bottom-right'
		});
	});
</script>
<title>硬件变更历史记录表</title>
</head>
<body class="background">
<div class="container-fluid white-background pagecontent">
<?
	if(isset($_SESSION['logname'])){
		if ($pagename=="changeitem"){
			echo "<label>操作项次: </label><a id='ADDOAKEY' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a> | <a id='ADDOAKEY' href='$pagestr'> <span class=\"glyphicon glyphicon-backward\"></span>返回</a>";
		}else{
			echo "<label>操作项次: </label><a id='ADDOAKEY' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a> | <a href='/export_hwchange.php'> <span class=\"glyphicon glyphicon-download-alt\"></span> 导出</a>";
		}
	}else{
		echo "<label>操作项次: </label><a href='/export_hwchange.php'> <span class=\"glyphicon glyphicon-download-alt\"></span> 导出</a>";
	}

	echo "<div id='ADDDIV'>\n";
	echo "<form action='$pagestr?act=add' method='post'>\n";
	echo "	<div>\n";
	echo "	添加变更内容:<br />\n";
	echo "		主板名称: <input name='mbname' id='mbname'><br>\n";
	echo "		主板版号: <input name='mbver' id='mbver'><br>\n";
	echo "		变更原因: <textarea name='mbreason' id='mbreason' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\"></textarea><br>\n";
	echo "		变更位置: <textarea name='mblocation' id='mblocation' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\"></textarea><br>\n";
	echo "		变更日期: <input name='mbchangedate' id='mbchangedate'><br>\n";
	echo "		备注说明: <textarea name='remark' id='remark' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\"></textarea><br>\n";
	echo "	</div>\n";
	echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit' id='addbtn'>\n<input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
	echo "</form>\n";
	echo "</div>\n";

	// change item page
	if ($pagename=="changeitem"){
		$db = new mysql();
		$sql="select * from hw_change where ID=$id";
		$db->query($sql);
		$num = $db->db_num_rows();
		$row = $db->fetch_assoc();
		echo "\n<form action='$pagestr?act=change&id=${row['ID']}' method='post'>\n";
		echo "	<div>\n";
		echo "	修改内容:<br />\n";
		echo "		主板名称: <input name='mbname' id='mbname' value='${row['MB_NAME']}'><br>\n";
		echo "		主板版号: <input name='mbver' id='mbver' value='${row['MB_VER']}'><br>\n";
		echo "		变更原因: <textarea name='mbreason' id='mbreason' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\">${row['MB_REASON']}</textarea><br>\n";
		echo "		变更位置: <textarea name='mblocation' id='mblocation' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\">${row['MB_LOCATION']}</textarea><br>\n";
		echo "		变更日期: <input name='mbchangedate' id='mbchangedate1' value='${row['MB_CHANGEDATE']}'><br>\n";
		echo "		备注说明: <textarea name='remark' id='remark' rows='6' cols='50' class=\"xheditor {tools:'Fullscreen',skin:'default'}\" style=\"width:30%;height:120px;\">${row['REMARK']}</textarea><br>\n";
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit'>\n";
		echo "  <input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		$db->close();
	}else{
		$db = new mysql();
		$db->query("select * from hw_change order by add_date desc");
		$num = $db->db_num_rows();
		echo "<table border=0 cellpadding=0 class='table table-striped'>";
		echo "<tr class='success'><th>主板名称</th><th>主板版号</th><th>变更原因</th><th>变更位置</th><th>变更日期</th><th>备注说明</th><th>负责人</th><th>添加日期</th><th>操作</th></tr>";
		for($i=0; $i<$num; $i++){
			$row = $db->fetch_assoc();
			echo "<tr>";
			echo "<td>${row['MB_NAME']}</td>";
			echo "<td>${row['MB_VER']}</td>";
			echo "<td>".htmlspecialchars_decode($row['MB_REASON'])."</td>";
			echo "<td>".htmlspecialchars_decode($row['MB_LOCATION'])."</td>";
			echo "<td>".substr($row['MB_CHANGEDATE'],0,10)."</td>";
			echo "<td>".htmlspecialchars_decode($row['REMARK'])."</td>";
			echo "<td>${row['OWNER']}</td>";
			echo "<td>${row['add_date']}</td>";
			if(isset($_SESSION['logname'])){
				echo "<td><a href='$pagestr?pagename=changeitem&id=${row['ID']}'><span class=\"glyphicon glyphicon-edit\"></span></a>";
				echo " | <a href='$pagestr?act=mail&id=${row['ID']}'><span class=\"glyphicon glyphicon-envelope\"></span></a>";
				echo "</td>";
			}else{
				echo "<td>#</td>";
			}
			echo "</tr>\n";
		}
		echo "</table>";
		$db->close();
	}
?>
</div>
</body>
</html>
